<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Elasticsearch Painless 脚本完全指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 8px 16px;
            font-size: 12px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e0e0e0, transparent);
            margin: 60px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            color: #667eea;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px 30px;
            border-radius: 16px;
            margin: 30px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .content-card {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.05);
            margin-bottom: 30px;
        }
        
        .icon-badge {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background: #f3f4f6;
            padding: 6px 16px;
            border-radius: 20px;
            font-size: 14px;
            color: #4b5563;
            margin-right: 10px;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white">
        <div class="container mx-auto px-6 py-20">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    Elasticsearch <span class="text-yellow-300">Painless</span> 脚本
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    掌握灵活强大的脚本功能，解锁 Elasticsearch 的无限可能
                </p>
                <div class="flex flex-wrap justify-center gap-4">
                    <span class="icon-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-bolt"></i> 高性能
                    </span>
                    <span class="icon-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-shield-alt"></i> 安全可靠
                    </span>
                    <span class="icon-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-code"></i> 简洁易用
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">E</span>lasticsearch 的脚本功能允许用户在查询和数据操作中自定义逻辑，提供了灵活性以应对复杂的需求。Elasticsearch 提供了多种脚本语言，其中 <strong class="text-purple-600">Painless</strong> 是最常用的脚本语言，它专为 Elasticsearch 设计，具有较高的性能和安全性。
            </p>
        </div>

        <!-- Painless Features -->
        <div class="content-card">
            <h2 class="text-3xl font-bold mb-8 gradient-text">1. Painless 脚本基础</h2>
            <p class="text-gray-700 mb-8">Painless 是一种基于 Java 的脚本语言，具有以下特点：</p>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="card-hover bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <div class="feature-icon bg-blue-500 text-white">
                        <i class="fas fa-rocket"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">高效</h3>
                    <p class="text-gray-600">优化了执行性能，适合在 Elasticsearch 中执行大量的脚本操作。</p>
                </div>
                
                <div class="card-hover bg-gradient-to-br from-green-50 to-emerald-50 p-6 rounded-xl">
                    <div class="feature-icon bg-green-500 text-white">
                        <i class="fas fa-lock"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">安全</h3>
                    <p class="text-gray-600">默认禁用不安全的操作，如访问文件系统和网络，防止潜在的安全问题。</p>
                </div>
                
                <div class="card-hover bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="feature-icon bg-purple-500 text-white">
                        <i class="fas fa-feather"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">简洁</h3>
                    <p class="text-gray-600">语法类似于 Java，但去除了部分复杂性，使得编写和阅读脚本更加简洁。</p>
                </div>
            </div>
        </div>

        <!-- Query Usage -->
        <div class="content-card">
            <h2 class="text-3xl font-bold mb-8 gradient-text">2. 在查询中使用 Painless 脚本</h2>
            <p class="text-gray-700 mb-6 text-lg">
                Painless 脚本可以在查询中用于复杂的过滤逻辑、评分计算和动态字段处理。例如，以下是一个使用 Painless 脚本进行自定义评分的示例：
            </p>
            
            <div class="code-block">
                <div class="code-header">
                    <i class="fas fa-code"></i> JSON
                </div>
                <pre><code>{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "title": "Elasticsearch"
        }
      },
      "script_score": {
        "script": {
          "source": "Math.log(2 + doc['popularity'].value)"
        }
      }
    }
  }
}</code></pre>
            </div>
            
            <div class="mt-6 bg-blue-50 p-6 rounded-xl">
                <p class="font-semibold text-blue-900 mb-3">
                    <i class="fas fa-info-circle mr-2"></i>代码解析：
                </p>
                <ul class="space-y-2 text-gray-700">
                    <li><i class="fas fa-chevron-right text-blue-500 mr-2"></i><code class="bg-white px-2 py-1 rounded">function_score</code> 查询用于根据脚本计算的分数来调整文档的评分。</li>
                    <li><i class="fas fa-chevron-right text-blue-500 mr-2"></i><code class="bg-white px-2 py-1 rounded">script_score</code> 使用 Painless 脚本 <code class="bg-white px-2 py-1 rounded">Math.log(2 + doc['popularity'].value)</code> 来计算评分。</li>
                </ul>
            </div>
        </div>

        <!-- Aggregation Usage -->
        <div class="content-card">
            <h2 class="text-3xl font-bold mb-8 gradient-text">3. 在聚合中使用 Painless 脚本</h2>
            <p class="text-gray-700 mb-6 text-lg">
                Painless 脚本可以用于聚合操作中进行复杂的数据处理和自定义计算。例如，计算某个字段的加权总和：
            </p>
            
            <div class="code-block">
                <div class="code-header">
                    <i class="fas fa-code"></i> JSON
                </div>
                <pre><code>{
  "aggs": {
    "total_weighted_score": {
      "sum": {
        "script": {
          "source": "doc['score'].value * params.weight",
          "params": {
            "weight": 1.5
          }
        }
      }
    }
  }
}</code></pre>
            </div>
            
            <div class="mt-6 bg-green-50 p-6 rounded-xl">
                <p class="font-semibold text-green-900 mb-3">
                    <i class="fas fa-lightbulb mr-2"></i>关键要点：
                </p>
                <ul class="space-y-2 text-gray-700">
                    <li><i class="fas fa-check text-green-500 mr-2"></i><code class="bg-white px-2 py-1 rounded">sum</code> 聚合使用脚本计算字段的加权总和。</li>
                    <li><i class="fas fa-check text-green-500 mr-2"></i><code class="bg-white px-2 py-1 rounded">params.weight</code> 是传递给脚本的参数，用于动态调整加权值。</li>
                </ul>
            </div>
        </div>

        <!-- Update Usage -->
        <div class="content-card">
            <h2 class="text-3xl font-bold mb-8 gradient-text">4. 在更新中使用 Painless 脚本</h2>
            <p class="text-gray-700 mb-6 text-lg">
                Painless 脚本在更新操作中也非常有用，特别是在需要根据现有数据计算新值时。例如，更新文档中的字段值：
            </p>
            
            <div class="code-block">
                <div class="code-header">
                    <i class="fas fa-code"></i> JSON
                </div>
                <pre><code>POST /my_index/_update/1
{
  "script": {
    "source": "ctx._source.views += params.add_views",
    "params": {
      "add_views": 10
    }
  }
}</code></pre>
            </div>
            
            <div class="mt-6 bg-purple-50 p-6 rounded-xl">
                <p class="font-semibold text-purple-900 mb-3">
                    <i class="fas fa-code-branch mr-2"></i>操作说明：
                </p>
                <ul class="space-y-2 text-gray-700">
                    <li><i class="fas fa-arrow-right text-purple-500 mr-2"></i><code class="bg-white px-2 py-1 rounded">ctx._source.views += params.add_views</code> 将文档中的 views 字段增加 10。</li>
                    <li><i class="fas fa-arrow-right text-purple-500 mr-2"></i><code class="